package com.example.rbac.controller;

import com.example.rbac.annotation.RequirePermission;
import com.example.rbac.common.Result;
import com.example.rbac.model.Permission;
import com.example.rbac.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/permission")
@Api(tags = "权限管理")
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;
    
    @GetMapping("/list")
    @ApiOperation("获取权限列表")
    @RequirePermission(value = "permission:list", name = "查看权限列表")
    public Result<?> getPermissionList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String permissionName) {
        Page<Permission> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        if (permissionName != null && !permissionName.isEmpty()) {
            queryWrapper.like("name", permissionName);
        }
        Page<Permission> permissionPage = permissionService.page(page, queryWrapper);
        return Result.success(permissionPage);
    }
    
    @GetMapping("/{id}")
    @ApiOperation("获取权限详情")
    @RequirePermission(value = "permission:info", name = "查看权限详情")
    public Result<?> getPermissionById(@PathVariable Long id) {
        Permission permission = permissionService.getById(id);
        if (permission == null) {
            return Result.error("权限不存在");
        }
        return Result.success(permission);
    }
    
    @PostMapping
    @ApiOperation("新增权限")
    @RequirePermission(value = "permission:add", name = "新增权限")
    public Result<?> addPermission(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.success("添加成功");
    }
    
    @PutMapping
    @ApiOperation("修改权限")
    @RequirePermission(value = "permission:edit", name = "修改权限")
    public Result<?> updatePermission(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.success("修改成功");
    }
    
    @DeleteMapping("/{id}")
    @ApiOperation("删除权限")
    @RequirePermission(value = "permission:delete", name = "删除权限")
    public Result<?> deletePermission(@PathVariable Long id) {
        permissionService.removeById(id);
        return Result.success("删除成功");
    }
    
    @PostMapping("/assign")
    @ApiOperation("给角色分配权限")
    @RequirePermission(value = "permission:assign", name = "分配权限")
    public Result<?> assignPermissions(@RequestBody Map<String, Object> params) {
        Long roleId = Long.parseLong(params.get("roleId").toString());
        List<Long> permissionIds = (List<Long>) params.get("permissionIds");
        permissionService.assignPermissionsToRole(roleId, permissionIds);
        return Result.success("权限分配成功");
    }
    
    @GetMapping("/all")
    @ApiOperation("获取所有接口权限信息")
    @RequirePermission(value = "permission:all", name = "查看所有权限")
    public Result<?> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        return Result.success(permissions);
    }
}